//
//  IsoUtil.h
//  IsoCplus
//
//  Created by 周张林 on 15/9/15.
//
//

#ifndef __IsoCplus__IsoUtil__
#define __IsoCplus__IsoUtil__

#include <cmath>

USING_NS_CC;

const float Y_CORRECT = std::cos(-3.1416f / 6) * std::sqrt(2);
//宽高比,0.75为coc地图，格式宽为40，高为30。0.5为正常的45度地图，格式宽为60，高为30
const float W_H_RATE = 0.75f ;

/// iso工具类
class IsoUtil
{
public:
    
    //iso坐标转到场景坐标
    static inline Vec2 isoToScreen(float px,float py ,float pz){
        auto screenX = px - pz;
        auto screenY = py * Y_CORRECT + (px + pz) * W_H_RATE;
        return Vec2(screenX, -screenY);
    };
    //场景坐标到iso地图像素坐标的转换
    static inline Vec2 screenToIso(float px,float py )
    {
        auto zpos=(-py-px*W_H_RATE)/(2*W_H_RATE);
        auto xpos = px+zpos;
        auto ypos = 0;
        return Vec2(xpos,ypos);
    }
    //场景坐标转到iso网格坐标
    static inline Vec2 screenToIsoGrid(float size,float px ,float py)
    {
        auto zpos = ( -py-px*W_H_RATE )/(2*W_H_RATE);
        auto xpos = px + zpos;
        
        auto col = std::floor ( xpos / size );
        auto row = std::floor ( zpos / size );
        return Vec2((int)col,(int)row);
    }
};


#endif /* defined(__IsoCplus__IsoUtil__) */
